﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MpSoft.Library.CommonLibrary
{
    public class Upload
    {
        public static string UploadImage(FileUpload FileUpload,string PathUpload)
        {
            Boolean fileOK = false;
            string result = "";
            String path = HttpContext.Current.Server.MapPath(PathUpload);
            if (FileUpload.HasFile)
            {
                String fileExtension =
                    System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }
            if (fileOK)
            {
                try
                {
                    FileUpload.PostedFile.SaveAs(path
                        + FileUpload.FileName);
                    result = PathUpload + FileUpload.FileName;                    
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }
            return result;
        }
    }
}
